package com.laycoding.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.laycoding.common.entity.Goods;
import com.laycoding.common.util.Result;
import com.laycoding.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * created with IntelliJ IDEA.
 *
 * @author: guidada
 * @date: 2021/06/04/10:19
 * @description:
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private GoodsService goodsService;
    @RequestMapping("/pushAll")
    public Result pushAll(){
        List<Goods> goods = goodsService.getBaseMapper().selectList(null);
        for (Goods good : goods) {
            Map<String,Object> map =new HashMap<>();
            map.put("stack",good.getGoodsStock());
            map.put("info", JSONObject.toJSON(good));
            redisTemplate.opsForHash().putAll("good:"+good.getId(),map);
        }
        return Result.OK(0,"上架成功","success");
    }

}
